<%@ page pageEncoding="UTF-8"%>
<%@ include file="/common/validate.jsp"%>
<%@ include file="/common/quest.jsp"%>
<%!
	// 查询热门新闻,按照浏览数+点赞数+评论数排序
	String hot( Input input) {
		Integer uid = input.getInt("uid", -1);
		Integer categoryId = input.getInt("category", -1); // 分组id, 默认全部分类问题
		String category = "";
		if (categoryId != -1)
			category = " and n.category=" + category;
		Integer hospital = input.getInt("hospital", -1); // 医院id
		Integer page = input.getInt("page", 1); // 下拉刷新
		Integer itemCount = input.getInt("item_count", 5); // 上拉加载
		org.hibernate.StatelessSession ss = DBTools.db("basedb").getSf().openStatelessSession();
		org.hibernate.SQLQuery sqlQuery = ss.createSQLQuery(String.format("SELECT n.id as id , n.author as author, n.news_id as news_id, n.url as url, n.title as title, n.summary as summary, n.time as time, n.img as img, n.json as json, n.tag as tag, n.category as category, n.branch_name as branch_name, count(o.oid) AS hot FROM news AS n,	operate_history AS o WHERE n.id = o.oid AND n.hospital =%s	AND o. STATUS = 1	AND n. STATUS = 1 GROUP BY o.oid order by  count(o.oid) desc", new Object[]{ hospital}));
		List<Map> newsList = sqlQuery.setFirstResult((page - 1) * itemCount).setMaxResults(itemCount).setResultTransformer(org.hibernate.transform.Transformers.ALIAS_TO_ENTITY_MAP).list();
		OperateHistory oh = new OperateHistory();
		oh.setHospital(hospital);
		oh.setCategory(NEWS_CATEGORY);
		oh.setOperate(LOVE);
		love(query(newsList, oh), uid, oh);
		List<Map> rankCount = ss.createSQLQuery(String.format("SELECT count(*) as size FROM news AS n,	operate_history AS o WHERE n.id = o.oid AND n.hospital =%s	AND o. STATUS = 1	AND n. STATUS = 1 GROUP BY o.oid ", new Object[]{hospital})).setResultTransformer(org.hibernate.transform.Transformers.ALIAS_TO_ENTITY_MAP).list();
		Integer size =  Tools.getInt(isEmpty(rankCount)?"0":(rankCount.get(0).get("size") + ""));
		if (ss != null) 
			ss.close();
		Message msg = new Message(0);
		Map resultMap = new HashMap();
		resultMap.put("rank", newsList);          
		resultMap.put("current_page", page);
		resultMap.put("page_count",  size%itemCount == 0?(size/itemCount):(size/itemCount + 1));
		resultMap.put("item_count",  itemCount);
		if (isEmpty(newsList))
			msg.setErrcodeAndErrmsg(30041, "没有更多了");
		msg.put("data", resultMap);
		return msg.toJson();
	}
%>
<%
	Tools.encryptPrint(out, hot(input));
%>
